﻿Imports MySql.Data.MySqlClient

Public Class Form1
    Private logginnfil As String = "mysqlinfo.txt"

    Dim db As MySqlConnection

    Private Function Sporring(ByVal sql As String) As DataTable

        Dim tabell As New DataTable

        Try

            db.Open()

            ' Gjøre sql-spørring og fylle tabell med data

            Dim kommando As New MySqlCommand(sql, db)

            Dim da As New MySqlDataAdapter
            da.SelectCommand = kommando
            da.Fill(tabell)

            db.Close()

        Catch ex As MySqlException
            MessageBox.Show("En feil oppstått - " & ex.Message)
        End Try

        Return tabell

    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' Lagre påloggingsdata til tekstfil
        FileOpen(1, logginnfil, OpenMode.Output)

        PrintLine(1, TextBox1.Text)     ' servernavn
        PrintLine(1, TextBox2.Text)     ' database
        PrintLine(1, TextBox3.Text)     ' brukernavn
        PrintLine(1, TextBox4.Text)     ' passord

        FileClose(1)

        MessageBox.Show("Innstillingene ble lagret")
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        db = New MySqlConnection()

        db.ConnectionString = "Server=" & TextBox1.Text & ";" &
            "Database=" & TextBox2.Text & ";" &
            "Uid=" & TextBox3.Text & ";" &
            "Pwd=" & TextBox4.Text & ";"

        Try

            db.Open()
            MessageBox.Show("Oppkoblingen var vellykket")
            db.Close()

        Catch ex As MySqlException
            MessageBox.Show("En feil oppstått - " & ex.Message)
        End Try
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            ' Les påloggingsdata fra tekstfil
            FileOpen(1, logginnfil, OpenMode.Input)

            TextBox1.Text = LineInput(1)
            TextBox2.Text = LineInput(1)
            TextBox3.Text = LineInput(1)
            TextBox4.Text = LineInput(1)

            FileClose(1)

        Catch ex As Exception
            MessageBox.Show("En feil oppstått - " & ex.Message)
        End Try
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        ListBox2.Items.Clear()

        Dim resultat As DataTable
        resultat = Sporring("SELECT DISTINCT m.rett_navn, i.ing_navn, i.maaleenhet, o.antall FROM OPPING o, INGREDIENSER i, MENY m WHERE m.meny_id = '" & txtrettID.Text & "' AND o.ing_id = i.ing_id")

        For Each rad In resultat.Rows
            ListBox2.Items.Add(rad("rett_navn") & vbTab & rad("ing_navn") & vbTab & vbTab & rad("antall") & vbTab & rad("maaleenhet"))
        Next
    End Sub

    Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged
        ListBox1.Items.Clear()
        ListBox3.Items.Clear()

        Dim resultat As DataTable
        resultat = Sporring("SELECT * FROM MENY WHERE rett_navn LIKE '%" & TextBox5.Text & "%'")

        For Each rad In resultat.Rows
            ListBox1.Items.Add(rad("rett_navn"))
            ListBox3.Items.Add(rad("meny_id"))
        Next
    End Sub
    Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
        txtrettNavn.Text = ListBox1.SelectedItem
        ListBox3.SelectedIndex = ListBox1.SelectedIndex
        txtrettID.Text = ListBox3.SelectedItem
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Form2.Show()
    End Sub
End Class
